package cn.touna.jss.modules.custom.carmodel.util;

import cn.touna.jss.modules.custom.carmodel.entity.Car300BrandEntity;
import cn.touna.jss.modules.custom.carmodel.entity.Car300ModelEntity;
import cn.touna.jss.modules.custom.carmodel.entity.Car300SeriesEntity;
import cn.touna.jss.modules.custom.carmodel.mapper.Car300BrandEntityMapper;
import cn.touna.jss.modules.custom.carmodel.mapper.Car300ModelEntityMapper;
import cn.touna.jss.modules.custom.carmodel.mapper.Car300SeriesEntityMapper;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * 用于导出车300车型车系的json
 * Created by Steve.Tao on 2017/8/15.
 */
public class CarModelExporter {

    private Car300BrandEntityMapper car300BrandEntityMapper;

    private Car300SeriesEntityMapper car300SeriesEntityMapper;

    private Car300ModelEntityMapper car300ModelEntityMapper;

    public CarModelExporter(Car300BrandEntityMapper car300BrandEntityMapper,
                            Car300SeriesEntityMapper car300SeriesEntityMapper,
                            Car300ModelEntityMapper car300ModelEntityMapper) {
        this.car300BrandEntityMapper = car300BrandEntityMapper;
        this.car300ModelEntityMapper = car300ModelEntityMapper;
        this.car300SeriesEntityMapper = car300SeriesEntityMapper;
    }

    public void export(String dirPath) throws IOException {
        File dir = new File(dirPath);
        if (!dir.isDirectory())
            throw new IllegalArgumentException("非文件目录");
        List<Car300BrandEntity> list = car300BrandEntityMapper.selectByPage(0, 0, null);
        JSONArray brandArray = new JSONArray();
        list.stream().forEach(brand -> {
            JSONObject item = new JSONObject();
            item.put("id", brand.getBrandId());
            item.put("name", brand.getBrandName());
            item.put("initial", brand.getInitial());
            brandArray.add(item);
        });
        write(dirPath + "/car.json", brandArray);

        for (Car300BrandEntity brand : list) {
            JSONObject brandJson = new JSONObject();
            List<Car300SeriesEntity> seriesEntityList = car300SeriesEntityMapper.selectByPage(brand.getBrandId(), 0, 0, null);
            brandJson.put("id", brand.getBrandId());
            brandJson.put("name", brand.getBrandName());
            brandJson.put("initial", brand.getInitial());

            JSONArray seriesArray = new JSONArray();
            for (Car300SeriesEntity series : seriesEntityList) {
                JSONObject seriesJson = new JSONObject();
                seriesJson.put("id", series.getSeriesId());
                seriesJson.put("groupName", series.getSeriesGroupName());
                seriesJson.put("name", series.getSeriesName());

                List<Car300ModelEntity> car300ModelEntities = car300ModelEntityMapper.selectByPage(series.getSeriesId(), 0, 0);
                JSONArray modelArray = new JSONArray();
                for (Car300ModelEntity model : car300ModelEntities) {
                    JSONObject modelJson = new JSONObject();
                    modelJson.put("id", model.getModelId());
                    modelJson.put("name", model.getModelName());
                    modelJson.put("liter", model.getLiter());
                    modelJson.put("year", model.getModelYear());
                    modelJson.put("gearType", model.getGearType());
                    modelArray.add(modelJson);
                }
                seriesJson.put("models", modelArray);
                seriesArray.add(seriesJson);
            }
            brandJson.put("series", seriesArray);
            write(dirPath + "/car" + brand.getBrandId() + ".json", brandJson);
        }
    }

    private void write(String filename, JSON json) {
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(filename);
            fileWriter.write(json.toJSONString());
            fileWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
